/**
 * 
 */
package com.pmsco.pms.core.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.pmsco.pms.core.dao.PmsMenuRoleDao;
import com.pmsco.pms.core.domain.PmsMenu;
import com.pmsco.pms.core.domain.PmsMenuRole;
import com.pmsco.pms.core.domain.PmsRole;

/**
 * @author Long Tran
 *
 */
public class PmsMenuRoleDaoImpl extends HibernateDaoSupport implements PmsMenuRoleDao {

	public void saveOrUpdatePmsMenuRole(PmsMenuRole pmsMenuRole) {
		getHibernateTemplate().merge(pmsMenuRole);
		getHibernateTemplate().flush();
	}

	public List<PmsMenuRole> listPmsMenuByRole(PmsMenuRole searchTemplate) {
		List<PmsMenuRole> ret = new ArrayList<PmsMenuRole>();
		Criteria crit = this.getSession().createCriteria(PmsMenuRole.class);
		crit.createAlias(PmsMenuRole.roleObj, PmsMenuRole.roleObj);
        crit.createAlias(PmsMenuRole.menuObj, PmsMenuRole.menuObj);
        crit.add(Restrictions.eq(PmsMenuRole.roleObj + "." + PmsRole.idField, searchTemplate.getRole().getId()));
        crit.addOrder(Order.asc(PmsMenuRole.menuObj + "." + PmsMenu.orderIdField));
		ret = crit.list();
		return ret;
	}
	
	public List<PmsMenuRole> loadPmsMenuRoleByMenuId(Long menuId) {
		List<PmsMenuRole> ret = new ArrayList<PmsMenuRole>();
		Criteria crit = this.getSession().createCriteria(PmsMenuRole.class);
        crit.createAlias(PmsMenuRole.menuObj, PmsMenuRole.menuObj);
        crit.add(Restrictions.eq(PmsMenuRole.menuObj + "." + PmsMenu.idField, menuId));
		ret = crit.list();
		return ret;
	}

	public PmsMenuRole loadPmsMenuRole(PmsMenuRole searchTemplate) {
		PmsMenuRole ret = null;
		List<PmsMenuRole> temp = new ArrayList<PmsMenuRole>();
		temp = getHibernateTemplate().findByExample(searchTemplate);
		if(temp != null && temp.size() == 1)
			ret = temp.get(0);
		return ret;
	}
	
	public void deletePmsMenuRole(PmsMenuRole pmsMenuRole) {
		getHibernateTemplate().delete(pmsMenuRole);
		getHibernateTemplate().flush();
	}

}
